#!/usr/bin/env python 
# coding:utf-8
# @Time :10/11/18 10:16

import sys

sys.path.append('../')
sys.path.append('../../')


from config.beanstalk_conf import BEANSTALK_CONF
from common.pybeanstalk import PyBeanstalk
from common.mongo import MongDb
from common.logger import AppLogger
from config.mongo_conf import MONGO_DB_SOURCE
from config.tube_conf import TUBE_INFO
import time

from beanstalkc import CommandFailed


class CompanyNameLoader(object):

    __POLL_CYCLE = 10

    __MAX_PUT_MSG = 3

    # 分批读取mongo名单数
    # __MONGO_BATCH = 1000

    def __init__(self, log=None):
        self.log = log
        self.beanstalk = PyBeanstalk(BEANSTALK_CONF['host'], BEANSTALK_CONF['port'])
        self.mongo = MongDb(MONGO_DB_SOURCE['host'], MONGO_DB_SOURCE['port'], MONGO_DB_SOURCE['db'],
                            MONGO_DB_SOURCE['username'], MONGO_DB_SOURCE['password'], self.log)

        self.last_expired_time = int(time.time())

        self.__tube = TUBE_INFO["wenshu_company_name_tube"]

        self.__max_beanstalk_block = 100

    def __cycle_expired(self):
        current_time = int(time.time())
        if current_time - self.last_expired_time > self.__POLL_CYCLE:
            self.log.info("__cycle_expired 开始新一轮轮询...")
            self.last_expired_time = current_time
            return True
        # self.log.info("__cycle_expired 没有过期")
        return False

    def __is_beanstalk_block(self):
        try:
            count = self.beanstalk.get_tube_count(self.__tube)
        except CommandFailed, (_, status, results):
            if status == "NOT_FOUND":
                self.log.error("__is_mq_blocked 获取消息队列不存在, tube={}".format(self.__tube))
                count = 0
            else:
                self.log.error("获取消息队列异常： tube={}, status={}".format(self.__tube, status))
                count = self.__max_beanstalk_block
        except Exception as e:
            count = self.__max_beanstalk_block
            self.log.error("获取消息队列大小失败：")
            self.log.exception(e)

        if count > self.__max_beanstalk_block:
            return True
        return False

    def start(self):
        sleep_count = 0
        while True:
            if self.__cycle_expired():
                if self.__is_beanstalk_block():
                    time.sleep(1)
                    sleep_count += 1
                    # self.log.info("sleep_count>>>>{}".format(sleep_count))
                    if sleep_count % 10 == 0:
                        self.log.warn("CompanyNameLoader 消息队列已经阻塞了 %s 秒，请校验任务..." % (sleep_count*self.__POLL_CYCLE))
                    continue

                cursor = self.mongo.traverse_batch("clue_lanzhou_company_list", {"wenshu_crawl_status": None})
                for per_cursor in cursor:
                    company = str(per_cursor.get("_id"))

                    # 进行多次尝试发送任务
                    for count in range(self.__MAX_PUT_MSG):
                        try:
                            if count > 0:
                                self.log.info("start 当前任务发送消息队列失败，正在尝试第 {} 次重试".format(count))

                            is_push_succeed = self.beanstalk.put(self.__tube, company)
                            self.log.info("{} 发送至成功...".format(company))

                            if is_push_succeed:
                                break

                            self.log.warn("当前任务发送失败，company={}".format(company))
                            time.sleep(1)
                        except Exception as e:
                            self.log.error("start 发送任务发生异常，错误原因是：")
                            self.log.exception(e)
                            time.sleep(1)


if __name__ == '__main__':
    log = AppLogger("CompanyNameLoader.log").get_logger()
    company_loader = CompanyNameLoader(log=log)
    company_loader.start()



